package com.ghovos.usermangerpgdemo.contorller.api;

import com.ghovos.usermangerpgdemo.common.result.GlobalResult;
import com.ghovos.usermangerpgdemo.entity.RoleEntity;
import com.ghovos.usermangerpgdemo.entity.RolePermissionEntity;
import com.ghovos.usermangerpgdemo.service.PermissionService;
import com.ghovos.usermangerpgdemo.service.RoleService;
import com.ghovos.usermangerpgdemo.vo.req.RoleMenuIdListReq;
import com.ghovos.usermangerpgdemo.vo.req.RolePermissionIdListReq;
import com.ghovos.usermangerpgdemo.vo.req.RoleUserIdListReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.jshell.PersistentSnippet;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * @author EdgarLiu(刘凌峰) <br/>
 * date: 2022/3/18 <br/>
 * time: 上午9:35 <br/>
 * email: EdgarLlf@foxmail.com
 */

@RestController
@RequestMapping("/role")
@AllArgsConstructor
@Api(tags = "角色管理api")
public class RoleController {

    private final RoleService roleService;
    private final PermissionService permissionService;

    @ApiOperation("根据id获取权限信息")
    @GetMapping("/find/{id}")
    public GlobalResult<Object> getById(@PathVariable("id") Long id) {
        return GlobalResult.ok(roleService.getById(id));
    }

    @ApiOperation("根据id获取权限信息包括用户列表")
    @GetMapping("/find/ext/{id}")
    public GlobalResult<Object> getExtById(@PathVariable("id") Long id) {
        return GlobalResult.ok(roleService.getRoleExtById(id));
    }

    @ApiOperation("权限列表")
    @GetMapping("/all")
    public GlobalResult<Object> all() {
        return GlobalResult.ok(roleService.list());
    }

    @ApiOperation("添加role")
    @PostMapping("/add")
    public GlobalResult<Object> add(@RequestBody RoleEntity roleEntity) {
        return GlobalResult.ok(roleService.save(roleEntity));
    }

    @ApiOperation("根据id修改role")
    @PostMapping("/update")
    public GlobalResult<Object> updateById(@RequestBody RoleEntity roleEntity) {
        return GlobalResult.ok(roleService.updateById(roleEntity));
    }

    @ApiOperation("根据id删除role, 连同连接表")
    @GetMapping("/delete/{id}")
    public GlobalResult<Object> deleteById(@PathVariable("id") Long id) {
        return GlobalResult.ok(roleService.removeByIdWithOtherEntity(id));
    }

    @ApiOperation("批量添加用户")
    @PostMapping("/add/users")
    public GlobalResult<Object> addUserByIds(@RequestBody RoleUserIdListReq roleUserIdListReq) {
        return GlobalResult.ok(roleService.addUserList(roleUserIdListReq.getRoleId(),roleUserIdListReq.getUserIdList()));
    }

    @ApiOperation("批量删除用户")
    @PostMapping("/delete/users")
    public GlobalResult<Object> deleteUserByIds(@RequestBody RoleUserIdListReq roleUserIdListReq) {
        return GlobalResult.ok(roleService.deleteUserList(roleUserIdListReq.getRoleId(),roleUserIdListReq.getUserIdList()));
    }

    @ApiOperation("批量添加菜单")
    @PostMapping("/add/menus")
    public GlobalResult<Object> addMenuByIds(@RequestBody RoleMenuIdListReq roleMenuIdListReq) {
        return GlobalResult.ok(roleService.addMenuList(roleMenuIdListReq.getRoleId(),roleMenuIdListReq.getMenuIdList()));
    }

    @ApiOperation("批量删除菜单")
    @PostMapping("/delete/menus")
    public GlobalResult<Object> deleteMenuByIds(@RequestBody RoleMenuIdListReq roleMenuIdListReq) {
        return GlobalResult.ok(roleService.deleteMenuList(roleMenuIdListReq.getRoleId(), roleMenuIdListReq.getMenuIdList()));
    }

    @ApiOperation("批量添加权限")
    @PostMapping("/add/permissions")
    public GlobalResult<Object> addPermissionByIds(@RequestBody RolePermissionIdListReq rolePermissionIdListReq) {
        boolean isSuccess = roleService.addPermissionList(rolePermissionIdListReq.getRoleId(), rolePermissionIdListReq.getPermissionIdList());
        permissionService.initRedisUrlRole();
        return GlobalResult.ok(
                isSuccess
        );
    }

    @ApiOperation("批量删除权限")
    @PostMapping("/delete/permissions")
    public GlobalResult<Object> deletePermissionByIds(@RequestBody  RolePermissionIdListReq rolePermissionIdListReq) {
        roleService.deletePermissionList(rolePermissionIdListReq.getRoleId(), rolePermissionIdListReq.getPermissionIdList());
        permissionService.initRedisUrlRole();
        return GlobalResult.ok(
        );
    }

}
